package com.xydtech.web.controller.system;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.IndustryTree;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.system.domain.SysGbIndustry;
import com.xydtech.system.service.ISysGbIndustryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 国标行业Controller
 *
 * @author JHON
 * @date 2021-09-17
 */
@Controller
@RequestMapping("/system/industry")
public class SysGbIndustryController extends BaseController {
    private String prefix = "system/industry";

    @Autowired
    private ISysGbIndustryService sysGbIndustryService;

    @RequiresPermissions("system:industry:view")
    @GetMapping()
    public String industry() {
        return prefix + "/industry";
    }

    /**
     * 查询国标行业树列表
     */
    @RequiresPermissions("system:industry:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysGbIndustry> list(SysGbIndustry sysGbIndustry) {
        List<SysGbIndustry> list = sysGbIndustryService.selectSysGbIndustryList(sysGbIndustry);
        return list;
    }

    /**
     * 导出国标行业列表
     */
    @RequiresPermissions("system:industry:export")
    @Log(title = "国标行业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysGbIndustry sysGbIndustry) {
        List<SysGbIndustry> list = sysGbIndustryService.selectSysGbIndustryList(sysGbIndustry);
        ExcelUtil<SysGbIndustry> util = new ExcelUtil<SysGbIndustry>(SysGbIndustry.class);
        return util.exportExcel(list, "industry");
    }

    /**
     * 新增国标行业
     */
    @GetMapping(value = {"/add/{id}", "/add/"})
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("sysGbIndustry", sysGbIndustryService.selectSysGbIndustryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存国标行业
     */
    @RequiresPermissions("system:industry:add")
    @Log(title = "国标行业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysGbIndustry sysGbIndustry) {
        return toAjax(sysGbIndustryService.insertSysGbIndustry(sysGbIndustry));
    }

    /**
     * 修改国标行业
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysGbIndustry sysGbIndustry = sysGbIndustryService.selectSysGbIndustryById(id);
        mmap.put("sysGbIndustry", sysGbIndustry);
        return prefix + "/edit";
    }

    /**
     * 修改保存国标行业
     */
    @RequiresPermissions("system:industry:edit")
    @Log(title = "国标行业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysGbIndustry sysGbIndustry) {
        return toAjax(sysGbIndustryService.updateSysGbIndustry(sysGbIndustry));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:industry:remove")
    @Log(title = "国标行业", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(sysGbIndustryService.deleteSysGbIndustryById(id));
    }

    /**
     * 选择国标行业树
     */
    @GetMapping(value = {"/selectIndustryTree/{industryCode}", "/selectIndustryTree/"})
    public String selectIndustryTree(@PathVariable(value = "industryCode", required = false) String industryCode, ModelMap mmap) {
        if (StringUtils.isNotNull(industryCode)) {
            SysGbIndustry sysGbIndustry = new SysGbIndustry();
            sysGbIndustry.setIndustryCode(industryCode);
            mmap.put("sysGbIndustry", sysGbIndustryService.selectSysGbIndustryList(sysGbIndustry).get(0));
        }
        return prefix + "/tree";
    }

    /**
     * 加载国标行业树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<IndustryTree> treeData() {
        List<IndustryTree> ztrees = sysGbIndustryService.selectSysGbIndustryTree();
        return ztrees;
    }
}
